﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;

namespace Sharepoint.Client
{
    /// <summary>
    /// A connection object, that allows to open a sharepoint site and perform
    /// queries
    /// </summary>
    public class SPConnection
    {
        const string _listDataSvc = "/_vti_bin/ListData.svc";

        Uri _uri;
        System.Net.CookieContainer _cookieContainer = new System.Net.CookieContainer();

        public Uri Site { get { return _uri; } }

        public string CookieHeader
        {
            get
            {
                return _cookieContainer.GetCookieHeader(_uri);
                //return new System.Collections.ObjectModel.ReadOnlyCollection<Cookie>(_cookieCollection.Cast<Cookie>().ToList());
            }
        }

        public SPConnection(Uri uri, CookieContainer authCookies)
        {
            _uri = uri;
            _cookieContainer = authCookies;
        }

        public SPConnection(Uri uri, string authCookies)
        {
            _uri = uri;
            _cookieContainer.SetCookies(uri, authCookies);
        }

        /// <summary>
        /// Retrieves the main stream in XML from the ListData.svc.
        /// </summary>
        /// <returns>A Stream with the list of items in the site.</returns>
        public async Task<Stream> RequestAsync()
        {
            return await RequestAsync("");
        }

        /// <summary>
        /// Performs a custom query and retrieves the result stream.
        /// </summary>
        /// <param name="query">a custom query for the ListData.svc</param>
        /// <returns>The result of the query as a stream.</returns>
        public async Task<Stream> RequestAsync(string query)
        {
            return await requestStreamAsync(query, null);
        }

        private async Task<Stream> requestStreamAsync(string query, string accept)
        {
            var bldr = new UriBuilder(_uri.ToString());
            bldr.Path += _listDataSvc + query;
            var request = HttpWebRequest.CreateHttp(bldr.Uri);
            request.Method = "GET";
            if (accept != null)
                request.Accept = accept;
            request.CookieContainer = _cookieContainer;
            var response = await request.GetResponseAsync();
            return response.GetResponseStream();
        }

        /// <summary>
        /// Same as <see cref="RequestAsync"/> but with a Json result.
        /// </summary>
        /// <returns>A Stream containing the result in Json.</returns>
        public async Task<Stream> RequestJsonAsync()
        {
            return await RequestJsonAsync("");
        }

        /// <summary>
        /// Performs a custom query and retrieves the result stream in Json.
        /// </summary>
        /// <param name="query">a custom query for the ListData.svc</param>
        /// <returns>The result of the query as a Json stream.</returns>
        public async Task<Stream> RequestJsonAsync(string query)
        {
            return await requestStreamAsync(query, "application/json");
        }
    }
}
